<?php
use Menory\SMS\Adapter\HuaXin\Auth;
use Menory\SMS\Adapter\HuaXin\SMSCenter;
use Menory\SMS\SMSMessage;

class SMSCenterTest extends PHPUnit_Framework_TestCase {

    /**
     * @dataProvider genData
     * @test
     * @param array $data
     */
    public function sendMessage($data) {

        date_default_timezone_set('Asia/Shanghai');

        $redis  = new \Redis();
        $redis->pconnect("192.168.1.220", "6379");
        $redis->auth("app123456");
        $redis->select(31);

        $smsCenter = new SMSCenter(new Auth("", "6789fa26ac724bf1b4a41501098feca1"));
        $smsCenter->setCache($redis);
        $smsCenter->add(new SMSMessage("register", "SMS20160810557"));
        $smsCenter->add(new SMSMessage("find",     "SMS20160810557"));

        /** @var \Menory\Standard\Error $err */
        list($code, $err) = $smsCenter->sendMessage($data["message_type"], $data["phone"]);
        if ($data["message_type"] == "find") { $this->assertNull($err);return ; }
        if ($err !== null) {
            $this->assertInstanceOf(\Menory\Standard\Error::class, $err);
            if ($data["message_type"] == "register1") {
                $this->assertEquals("不存在的短信模板：register1", $err->getMessage());
            }
            elseif ($data["message_type"] == "register") {
                $this->assertEquals("请求格式错误", $err->getMessage());
            }
            var_dump($err->getMessage());
            return ;
        }

        $this->assertNotNull($code);

    }

    public function genData() {

        return [
            [[
                "message_type" => "register1",
                "phone"        => "156",
            ]],
            [[
                "message_type" => "register",
                "phone"        => "1812271104",
            ]],
            [[
                "message_type" => "find",
                "phone"        => "18122711047",
            ]]
        ];

    }

}
